package com.shinewind.home.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shinewind.home.mapper.ICartMapper;
import com.shinewind.home.service.ICartService;
import com.shinewind.pojo.dto.CartGoodDto;
import com.shinewind.pojo.entity.Cart;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl extends ServiceImpl<ICartMapper, Cart> implements ICartService {
    @Override
    public List<Cart> getCartList(String uId) {
        List<Cart> cartList = this.lambdaQuery().eq(Cart::getUId,uId).orderByDesc(Cart::getCreateTime).list();
        return cartList;
    }

    @Override
    public Boolean saveCartGoodIfNotExist(CartGoodDto cartGoodDto) {
        if(this.lambdaQuery().eq(Cart::getMId,cartGoodDto.getMId()).eq(Cart::getUId,cartGoodDto.getUId()).count() == 0){
            Cart cart = Cart.builder().build();
            BeanUtils.copyProperties(cartGoodDto,cart);
            return this.save(cart);
        }
        return false;
    }

    @Override
    public Boolean deleteCartGoodBycId(String uId,String cId) {
        Boolean flag = this.lambdaUpdate().eq(Cart::getUId,uId).eq(Cart::getCId,cId).remove();
        return flag;
    }
}